package com.lv.phuotbacnam.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

import com.lv.phuotbacnam.R;
import com.lv.phuotbacnam.bl.callback.CallBackFromParse;
import com.lv.phuotbacnam.bl.data.DiaDiemObject;
import com.lv.phuotbacnam.bl.log.Logger;
import com.lv.phuotbacnam.bl.queries.BaiDangQueries;
import com.lv.phuotbacnam.bl.queries.DiaDiemQueries;
import com.lv.phuotbacnam.bl.queries.QueriesConstant;
import com.lv.phuotbacnam.bl.utils.IntentHelper;
import com.lv.phuotbacnam.ui.Section;
import com.lv.phuotbacnam.ui.activity.BackActivity;
import com.lv.phuotbacnam.ui.adapter.HorizontalListViewAdapter;
import com.lv.phuotbacnam.ui.adapter.HorizontalListViewAdapter3;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.sileria.android.view.HorzListView;

public class MainFragment extends BaseFragment {

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		final View rootView = inflater.inflate(R.layout.fragment_main, container,
				false);
		final TextView tv_diadiemhot,tv_baimoidang,tv_xemtatcadiadiem;
		tv_diadiemhot = (TextView)rootView.findViewById(R.id.textView1);
		tv_baimoidang = (TextView)rootView.findViewById(R.id.tv_baimoidang);
		tv_xemtatcadiadiem = (TextView)rootView.findViewById(R.id.tv_xemtatcadiadiem);
        final HorzListView listview = (HorzListView) rootView.findViewById(R.id.lv_diadiemhot);
        final HorzListView listview_baidangmoi = (HorzListView) rootView.findViewById(R.id.lv_baimoidang);
		final ArrayList<MyItem> arr = new ArrayList<MyItem>();
		final ArrayList<ParseObject> arrParseObject = new ArrayList<ParseObject>();
		final ArrayList<ParseObject> arrParseObject_baidang = new ArrayList<ParseObject>();
		final ArrayList<MyItem> arrBaidangmoi = new ArrayList<MyItem>();
		final LayoutParams lparams = new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		
		//xem tat ca dia diem
		tv_xemtatcadiadiem.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(rootView.getContext(), Xemtatcadiadiem.class);
				startActivity(intent); 				
			}
		});

		//listview Ä‘á»‹a Ä‘iá»ƒm cĂ³ sá»‘ láº§n ghĂ© thÄƒm nhiá»�u nháº¥t
		DiaDiemQueries.getDiaDiemMostVisited(new CallBackFromParse<Object, List<ParseObject>>() { 
			@Override
			public void done(Object request, List<ParseObject> diadiems) {
				tv_diadiemhot.setText("Ä�ì£a Ä‘iĂª̀‰m hot");
				for(ParseObject diadiem: diadiems){
					arrParseObject.add(diadiem);
					//Logger.logMessage("Ten dia diem : "+ diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem)
					//		+ " | mo ta : "+ diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_mo_ta)
					//		);					
					// get list photo
					JSONArray list_photo = diadiem.getJSONArray(QueriesConstant.TableDiaDiem.COLUMN_list_photo);
					//Logger.logMessage("ListPhoto:"+ list_photo);
					
					try {
						JSONObject first_photo = list_photo.getJSONObject(0);
						//first_photo.
						String a = first_photo.getString("url");
						MyItem item = new MyItem(diadiem.getString(QueriesConstant.TableDiaDiem.COLUMN_ten_dia_diem), a);
						arr.add(item);
						//Logger.logMessage("firstPhoto:"+ a);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						//Logger.logMessage("khong co gi");
					}
					
					
					//int luot_visited = diadiem.getInt(QueriesConstant.TableDiaDiem.COLUMN_luot_visited);
					
					//ParseUser nguoitao = diadiem.getParseUser(QueriesConstant.TableDiaDiem.COLUMN_nguoi_tao);
					
					
					HorizontalListViewAdapter adapterImg = new HorizontalListViewAdapter(arrParseObject, getActivity());
					listview.setAdapter(adapterImg);
					
					//xá»­ lĂ½ sá»± kiá»‡n onclick vĂ o list Ä‘á»‹a Ä‘iá»ƒm, má»Ÿ ra activity "chitietdiadiem2.class"
					listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
						@Override
						public void onItemClick(AdapterView<?> arg0, View arg1,
								int arg2, long arg3) {
							
							 Intent intent = new Intent(rootView.getContext(), ChitietDiadiem2.class);
							 IntentHelper.addObjectForKey(
										arrParseObject.get(arg2),"diadiem");
							 //Bundle bundle = new Bundle();
							 //bundle.putString("tendiadiem", arr.get(arg2).getTen());
							 //intent.putExtra("MyPackage", bundle);
							 startActivity(intent);
						}			
						
					});
					
				}
				
			}
		});
		
		//listview bĂ i Ä‘Äƒng má»›i nháº¥t
		BaiDangQueries.getBaiDangNewest(new CallBackFromParse<Object, List<ParseObject>>() {
			
			@Override
			public void done(Object request, List<ParseObject> baidangs) {
				tv_baimoidang.setText("BĂ i má»›i Ä‘Äƒng");
				for(ParseObject baidang: baidangs )
				{
					arrParseObject_baidang.add(baidang);
					JSONArray list_photo = baidang.getJSONArray(QueriesConstant.TableBaiDang.COLUMN_list_photo);
					//Logger.logMessage("ListPhoto:"+ list_photo);
					
					try {
						JSONObject first_photo = list_photo.getJSONObject(0);
						//first_photo.
						String a = first_photo.getString("url");
						MyItem item = new MyItem(baidang.getString(QueriesConstant.TableBaiDang.COLUMN_ten_bai_dang), a);
						arrBaidangmoi.add(item);
						//Logger.logMessage("firstPhoto:"+ a);
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						//Logger.logMessage("khong co gi");
					}
					
					
					//int luot_visited = diadiem.getInt(QueriesConstant.TableDiaDiem.COLUMN_luot_visited);
					
					//ParseUser nguoitao = baidang.getParseUser(QueriesConstant.TableBaiDang.);
					
					
					HorizontalListViewAdapter3 adapterImg = new HorizontalListViewAdapter3(arrParseObject_baidang, getActivity());
					listview_baidangmoi.setAdapter(adapterImg);
					listview.setOnItemClickListener(new OnItemClickListener() {

						@Override
						public void onItemClick(AdapterView<?> parent,
								View view, int position, long id) {
							//Intent intent = new Intent(rootView.getContext(), ChitietDiadiem.class);
							//IntentHelper.addObjectForKey(arrParseObject_baidang.get(position),"baidang");
							//startActivity(intent);

							
						}
						
					});
				
				}
				
			}
		});
		
		
		return rootView;
	}
	

	
}
